package com.yanqu.road.server.manager.activity.commonwish;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.CommonWishProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonWishDrawActivityMgr extends TempCrossMgr {

    private static Map<Integer, CommonWishDrawActivity> activityMap;
    
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        activityMap = new ConcurrentHashMap<>();
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossActivityMapByType(eActivityType.CommonWishDraw.getValue());
        for (Map.Entry<Integer, ActivityInfo> entry : activityInfoMap.entrySet()) {
            CommonWishDrawActivity activity = new CommonWishDrawActivity(entry.getValue());
            activity.init();
            activityMap.put(entry.getKey(), activity);
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        if(activityMap != null){
            for (Map.Entry<Integer, CommonWishDrawActivity> entry : activityMap.entrySet()) {
                entry.getValue().save();
            }
        }
        return true;
    }

    public static void addDrawNotice(int activityId, long serverId, long playerId, List<CommonWishProto.CommonWishNoticeMsg> noticeList) {
        CommonWishDrawActivity activity = getCommonWishActivity(activityId);
        if(activity != null){
            activity.addDrawNotice(serverId, playerId, noticeList);
        }
    }

    public static CommonWishProto.CommonWishNoticeRespMsg.Builder getNoticeList(int activityId) {
        CommonWishDrawActivity activity = getCommonWishActivity(activityId);
        if(activity != null){
            return activity.getNoticeList();
        }
        return CommonWishProto.CommonWishNoticeRespMsg.newBuilder().setRet(0);
    }

    public static CommonWishDrawActivity getCommonWishActivity(int activityId){
        if(activityMap.containsKey(activityId)){
            return activityMap.get(activityId);
        }
        synchronized (activityMap){
            if(activityMap.containsKey(activityId)){
                return activityMap.get(activityId);
            }
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            if(activityInfo == null){
                activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            }
            if(activityInfo != null){
                CommonWishDrawActivity activity = new CommonWishDrawActivity(activityInfo);
                activity.init();
                activityMap.put(activityId, activity);
            }
        }
        return activityMap.get(activityId);
    }
    
    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> acList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (Map.Entry<Integer, CommonWishDrawActivity> activityEntry : activityMap.entrySet()) {
            CommonWishDrawActivity activity = activityEntry.getValue();
            ActivityInfo activityInfo = activity.getActivityInfo();
            if((nowTime > activityInfo.getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                acList.add(activityInfo.getActivityId());
            }
        }
        for(Integer activityId : acList){
            activityMap.remove(activityId);
        }
        return true;
    }
}
